from django.shortcuts import render,HttpResponse,redirect
import requests
from app01.models import Department,UserInfo

# Create your views here.

def index(request):
    return HttpResponse("欢迎使用")

def user_list(request):
    return render(request,"user_list.html")

def tpl(request):
    name="xingxingru"
    roles = ["管理员","CEO","保安"]
    user_info = {
        "name":"茹星",
        "salary":1000000,
        "role":"CEO"
    }
    data_list=[
        {"name":"张三","salary":1000000,"role":"CEO"},
        {"name": "张四", "salary": 2000000, "role": "CEO"},
        {"name": "张五", "salary": 3000000, "role": "CEO"}
    ]
    return  render(request,"tpl.html",{
        "n1":name,
        "n2":roles,
        "n3":user_info,
        "n4":data_list
    })

def news(request):    
    res = requests.get("https://www.10086.cn/aboutus/news/groupnews/5018449_5585_11769.json?nowtime=1739353972131")
    res = res.json()
    print(res)
    return render(request,"news.html",{"n1":res})

def something(request):
    # request是一个对象，封装了用户发送过来的所有请求相关数据
    print(request.method)
    # 2.在URL上传递值 /something/?n1=123&n2=234
    print(request.GET)
    # 3.在请求体中提交数据
    print(request.POST)
    # 4.【响应】内容字符串返回给请求者
    # return HttpResponse("返回内容")
    # 5.【响应】读取HTML的内容 + 渲染（替换）-> 字符串，返回给用户浏览器
    # return render(request,"something.html",{"name":"张三"})
    # 6.【响应】浏览器重定向到其他页面
    return redirect("https://www.baidu.com")

def login(request):
    if request.method == "GET":
        return render(request,"login.html")
    else:
        print(request.POST)
        username = request.POST.get("user")
        password = request.POST.get("pwd")
        if username == 'root' and password == '123':
            return redirect("https://www.baidu.com/")
        else:
            return render(request,"login.html",{"error_msg":"用户名或密码错误"})
        
def orm(request):
    # 测试ORM操作表中数据
    # 1、新增
    Department.objects.create(title="销售部")
    Department.objects.create(title="IT部")
    Department.objects.create(title="运营部")
    # 2、删除
    Department.objects.filter(id=1).delete()
    # Department.objects.all().delete()
    # 3、获取数据
    data_list = UserInfo.objects.all()
    print(data_list)
    for obj in data_list:
        print(obj.id,obj.name,obj.password,obj.age)
    data_list = UserInfo.objects.filter(id=1)
    data_row = UserInfo.objects.filter(id=1).first()
    # 4、更新
    Department.objects.all().update(title = "123")
    Department.objects.filter(id=1).update(title = "123")
    return HttpResponse("成功")

def info_list(request):
    data_list = UserInfo.objects.all()
    return render(request,"info_list.html",{"data_list":data_list})

def info_add(request):
    if request.method == "GET":
        return render(request,"info_add.html")
    user = request.POST.get("user")
    pwd = request.POST.get("pwd")
    age = request.POST.get("age")
    UserInfo.objects.create(name=user,password=pwd,age=age)
    # return HttpResponse("添加成功")
    return redirect("/info/list")

def info_delete(request):
    nid = request.GET.get("nid")
    UserInfo.objects.filter(id=nid).delete()
    # return HttpResponse("删除成功")
    return redirect("/info/list")